package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.BiddingDemand;
import com.ruoyi.system.domain.SysUserOrder;
import com.ruoyi.system.mapper.BiddingDemandMapper;
import com.ruoyi.system.mapper.BiddingDemandSelectionMapper;
import com.ruoyi.system.mapper.SysUserOrderMapper;
import com.ruoyi.system.service.SysUserOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class SysUserOrderServiceImpl implements SysUserOrderService {
    @Resource
    private SysUserOrderMapper sysUserOrderMapper;
    @Resource
    private BiddingDemandMapper biddingDemandMapper;

    @Resource
    private BiddingDemandSelectionMapper biddingDemandSelectionMapper;

    /**
     * @param sysUserOrder 订单实体类对象
     * @return 返回查询到的数据
     * @Deprecated 查询我的订单分页接口
     */
    @Override
    public List<SysUserOrder> selectMyOrderPageList(SysUserOrder sysUserOrder) {
        return sysUserOrderMapper.selectMyOrderPageList(sysUserOrder);
    }

    /**
     * @param sysUserOrder 订单实体类对象
     * @param loginUser    当前登录用户
     * @return 返回成功或失败的数据
     * @Deprecated 派送订单给投标人
     */
    @Override
    @Transactional
    public AjaxResult addOrder(SysUserOrder sysUserOrder, LoginUser loginUser) {
        // 避免重复派送
        int cont = sysUserOrderMapper.queryRepeatData(sysUserOrder);
        if (cont != 0)
            return AjaxResult.error("无法重复派送");
        // 插入我的订单表
        sysUserOrder.setOrderId(UUID.randomUUID().toString().replaceAll("-", ""));
        sysUserOrder.setCreateTime(new Date());
        sysUserOrder.setOrderStatus("0");
        sysUserOrder.setIsSuccess("0");
        sysUserOrder.setIsOk("0");
        sysUserOrder.setCreateBy(loginUser.getUsername());
        // 获取任务的开始时间和结束时间
        BiddingDemand query = new BiddingDemand();
        query.setBiddingDemandId(sysUserOrder.getBiddingDemandId());
        query = biddingDemandMapper.selectByDemandId(query);
        if (query == null)
            return AjaxResult.error("订单不存在或已被删除!");
        sysUserOrder.setBiddingDemandStartTime(query.getBiddingDemandStartTime());
        sysUserOrder.setBiddingDemandEndTime(query.getBiddingDemandEndTime());
        sysUserOrderMapper.insert(sysUserOrder);
        // 修改投标任务状态
        query.setStatus("5");
        biddingDemandMapper.updateByPrimaryKey(query);
        return AjaxResult.success();
    }

    /**
     * @param sysUserOrder 订单实体类对象
     * @param loginUser    当前登录用户
     * @return 返回成功或失败的数据
     * @Deprecated 订单通过或拒绝按钮
     */
    @Override
    @Transactional
    public AjaxResult orderPassOrRefuse(SysUserOrder sysUserOrder, LoginUser loginUser) {
        // 通过订单id查询订单
        SysUserOrder queryOn = sysUserOrderMapper.selectByPrimaryKey(sysUserOrder.getOrderId());
        if (sysUserOrder.getPassOrRefuse().equals("0")) {
            // 拒绝逻辑
            queryOn.setOrderStatus("2");
            // 删除自己的订单表里面的数据
            sysUserOrderMapper.deleteByPrimaryKey(sysUserOrder.getOrderId());
            // 删除投标选标中关于自己的信息
            biddingDemandSelectionMapper.deleteByUserId(queryOn.getUserId());
            // 根据投标选标中的数据来设置投标任务状态
            int i = biddingDemandSelectionMapper.selectTotalCount(queryOn.getBiddingDemandId());
            BiddingDemand biddingData = getBiddingData(queryOn.getBiddingDemandId());
            if (i > 0){
                // 如果投标人数大于0 设置投标任务状态为选标中
                biddingData.setStatus("1");
            }else {
                // 如果投标人数小于0 设置投标任务状态为招标中
                biddingData.setStatus("0");
            }
            biddingDemandMapper.updateByPrimaryKey(biddingData);
        } else {
            // 同意逻辑
            queryOn.setOrderStatus("1");
            sysUserOrderMapper.updateByPrimaryKey(queryOn);
            // 查询投标任务信息
            BiddingDemand biddingData = getBiddingData(queryOn.getBiddingDemandId());
            // 设置状态为进行中
            biddingData.setStatus("2");
            // 修改投标任务状态
            biddingDemandMapper.updateByPrimaryKey(biddingData);
            // 短信通知
        }
        return AjaxResult.success("操作成功!");
    }


    BiddingDemand getBiddingData(Long biddingDemandId) {
        BiddingDemand queryOnBidding = new BiddingDemand();
        queryOnBidding.setBiddingDemandId(biddingDemandId);
        return biddingDemandMapper.selectByDemandId(queryOnBidding);
    }
}
